//
//  SystemMessageViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/27.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "SystemMessageViewController.h"

#import "SystemMessageTableViewCell.h"
#import "FriendInfoView.h"

static NSString * const kSystemMessageTableViewCellIdentifier = @"systemMessageTableViewCellIdentifier";

@interface SystemMessageViewController ()<UITableViewDataSource>

@property (nonatomic, strong) UITableView * systemMessageTableView; // 系统消息表格视图
@property (nonatomic, strong) NSArray * fDataSource; // 好友申请数据
@property (nonatomic, strong) NSArray * mDataSource; // 其他数据

@end

@implementation SystemMessageViewController

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加视图
    [self.view addSubview:self.systemMessageTableView];
    
    // 加载数据
    [self loadData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 自动布局
    [_systemMessageTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(SCREEN_HEIGHT * 0.092);
        make.bottom.offset(SCREEN_HEIGHT * -0.09);
        make.left.offset(SCREEN_WIDTH * 0.195);
        make.right.offset(0);
    }];
}

#pragma mark 🌷🌷🌷UITableViewDataSource🌷🌷🌷
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _fDataSource.count + _mDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kSystemMessageTableViewCellIdentifier forIndexPath:indexPath];

    // 单元格赋值
    cell.nameLabel.text = indexPath.row < _fDataSource.count ? [NSString stringWithFormat:@"%@ 请求添加您为好友", _fDataSource[indexPath.row][@"friendName"]] : _mDataSource[indexPath.row - _fDataSource.count][@"msg"];
    cell.dateLabel.text = indexPath.row < _fDataSource.count ? _fDataSource[indexPath.row][@"addTimes"] : _mDataSource[indexPath.row - _fDataSource.count][@"postTime"];
    cell.friendid = indexPath.row < _fDataSource.count ? _fDataSource[indexPath.row][@"friendid"] : _mDataSource[indexPath.row - _fDataSource.count][@"id"];
    cell.agreeButton.hidden = indexPath.row >= _fDataSource.count;
    cell.refuseButton.hidden = indexPath.row >= _fDataSource.count;
    cell.lookFriendInfoButton.hidden = indexPath.row >= _fDataSource.count;
    cell.deleteButton.hidden = indexPath.row < _fDataSource.count;
    
    // 处理请求成功回调
    [cell reloadData:^(NSString *friendid) {
        if (!friendid) {
            [self loadData];
            return ;
        }
        
        // 添加好友信息视图
        FriendInfoView * friendInfoView = [[FriendInfoView alloc] initWithFriendid:friendid];
        [self.view addSubview:friendInfoView];
        
        // 自动布局
        [friendInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(self.view);
            make.center.equalTo(self.view);
        }];
    }];
    
    return cell;
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (void)loadData {
    NSMutableDictionary * paramters = [NSMutableDictionary dictionary];
    [paramters setObject:[LoginUserInfo defaultUserInfo].userid forKey:@"uid"];
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(PERSONALHOMEPAGE_SYSTEMMESSAGE_URL) parameters:paramters finish:^(id responseObject, NSError *error) {
        if (!error) {
            if ([responseObject[@"status"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"加载完成！"];
                _fDataSource = responseObject[@"fData"];
                _mDataSource = responseObject[@"mData"];
                [_systemMessageTableView reloadData];
            } else {
                [MBProgressHUD showSuccess:@"加载失败！"];
            }
        } else {
            [MBProgressHUD showError:[[NSString stringWithFormat:@"%@", error] containsString:@"The request timed out."] ? @"登录超时" : @"I don't know"];
        }
    }];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UITableView *)systemMessageTableView {
    if (!_systemMessageTableView) {
        _systemMessageTableView = [[UITableView alloc] init];
        
        // 设置数据源
        _systemMessageTableView.dataSource = self;
        // 设置单元格高度
        _systemMessageTableView.rowHeight = SCREEN_HEIGHT * 0.12;
        // 去除分隔线
        _systemMessageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        // 注册单元格
        [_systemMessageTableView registerClass:[SystemMessageTableViewCell class] forCellReuseIdentifier:kSystemMessageTableViewCellIdentifier];
    }
    return _systemMessageTableView;
}

@end
